package caat.framework.utils;

import java.util.*;

import java.text.SimpleDateFormat;
import java.text.ParsePosition;

public class StringUtil {

    public static String IntToStr(int number) {
        return Integer.toString(number);
    }

    public static int StrToInt(String str) {
        Integer in = new Integer(str);
        return in.intValue();
    }

    public static void main(String[] args) {
//      String strValue = formatCurrency("100000000");
//      System.out.println(strValue);
        String str = "23122011";
      str = str.substring(0, 2)+ "/" + str.substring(2, 4) + "/" + str.substring(4, 8);
      
      System.out.println(str +" = "+str);
    }

    public static boolean stringToBoolean(String context) {
        boolean booleanValue = false;
        if ((context != null) && (context.trim().length() > 0)) {
            if ((context.equalsIgnoreCase("yes")) ||
                (context.equalsIgnoreCase("T"))) {
                booleanValue = true;
            } else if ((context.equalsIgnoreCase("no")) ||
                       (context.equalsIgnoreCase("F"))) {
                booleanValue = false;
            } else {
                //  System.out.println("Unable to convert: defaulting to false");
                booleanValue = false;
            }
        }
        return booleanValue;
    }

    public static String booleanToString(boolean booleanValue) {
        if (booleanValue)
            return "yes";
        return "no";
    }

    public static int booleanStringToInt(String context) {
        int value = 0;
        if ((context != null) && (context.trim().length() > 0)) {
            if (context.equalsIgnoreCase("yes")) {
                value = 1;
            } else if (context.equalsIgnoreCase("no")) {
                value = 0;
            } else {
                value = 0;
            }
        }
        return value;
    }

    public static String intToBooleanString(int value) {
        if (value == 1)
            return "yes";
        return "no";
    }

    public static String charToBooleanString(String context) {
        String value = "no";
        if ((context != null) && (context.trim().length() > 0)) {
            if (context.equalsIgnoreCase("Y")) {
                value = "yes";
            }
        }
        return value;
    }

    public static String booleanStringToChar(String context) {
        String value = "N";
        if ((context != null) && (context.trim().length() > 0)) {
            if (context.equalsIgnoreCase("YES")) {
                value = "Y";
            } else if (context.equalsIgnoreCase("CHECKED")) {
                value = "Y";
            }
        }
        return value;
    }

    public static String DateToString(Date d, String formatter) {
        if (d == null) {
            return null;
        } else {
            SimpleDateFormat fm = new SimpleDateFormat(formatter);
            String dateString;
            dateString = fm.format(d);
            return dateString;
        }
    }

    public static Date StringToDate(String myString, String formatter) {
        if (myString == null) {
            return null;
        } else {
            SimpleDateFormat fm = new SimpleDateFormat(formatter);
            ParsePosition pos = new ParsePosition(0);
            Date d = fm.parse(myString, pos);
            return d;
        }
    }

    public static String getCurrentDate() {
        Date date = new Date();
        return DateToString(date, "dd/MM/yyyy");
    }
    public static String getPreviousNextDate(int numberDay){        
        int MILLIS_IN_DAY = 1000 * 60 * 60 * 24;
        Date date = new Date();
        SimpleDateFormat dateFormat = new SimpleDateFormat("dd/MM/yyyy");
//        String prevDate = dateFormat.format(date.getTime() - MILLIS_IN_DAY);
//        String currDate = dateFormat.format(date.getTime());
//        String nextDate = dateFormat.format(date.getTime() + MILLIS_IN_DAY);
        String resultDate = "";
        if(numberDay < 0){
          resultDate = dateFormat.format(date.getTime() + MILLIS_IN_DAY*numberDay);
        }else if(numberDay < 0){
          resultDate = dateFormat.format(date.getTime() - MILLIS_IN_DAY*numberDay);
        }else if(numberDay == 0){
          resultDate = dateFormat.format(date.getTime() - MILLIS_IN_DAY*numberDay);
        }
        return resultDate;
    }

    public static String formatCurrency(String inValue) {

        if (inValue == null) {
            return "";
        }
        if (inValue.equals("")) {
            return "";
        }
        int i = inValue.indexOf(",");
        String outValue = "";
        if (i != -1) {
            outValue = inValue.substring(i);
            inValue = inValue.substring(0, i);
        }
        while (inValue.length() > 3) {
            outValue =
                    "." + inValue.substring(inValue.length() - 3, inValue.length()) +
                    outValue;
            inValue = inValue.substring(0, inValue.length() - 3);
        }
        if (inValue.equals("") || inValue.equals("-")) {
            outValue = inValue + outValue.substring(1);
        } else {
            outValue = inValue + outValue;
        }
        return outValue;
    }
    //KhangDQ
    public static String formatCurrency(String inValue, int nt_id) {
      if (inValue == null) {
          return "";
      }
      if (inValue.equals("")) {
          return "";
      }
      int i = -1;
      if(nt_id == 2){
        i = inValue.indexOf(",");
      }else{
        i = inValue.indexOf(".");
      }
      String outValue = "";
      if (i != -1) {
          outValue = inValue.substring(i);
          inValue = inValue.substring(0, i);
      }
      while (inValue.length() > 3) {
          if(nt_id == 2)
            outValue = "." + inValue.substring(inValue.length() - 3, inValue.length()) + outValue;
          else if(nt_id != 2)
            outValue = "," + inValue.substring(inValue.length() - 3, inValue.length()) + outValue;
          inValue = inValue.substring(0, inValue.length() - 3);
      }
      if (!inValue.equals("")) {
        outValue = inValue + outValue; 
      } else {
        if(outValue.length() > 0)
          outValue = inValue + outValue.substring(1);
        else
          outValue = inValue;  
      }
      
      return outValue;
  }

    // Added by developers
    // HanPD

    /**
     * Chuyen mot arraylist thanh xau chua cac
     * thanh phan duoc phan cach boi dau "'"
     *
     * @author    Phung Duy Han
     * @param     coll - Danh sach cac id cua cac header duoc chon
     * @return        Collection - Danh sach cac xau thanh phan
     * @since     04/01/2003
     */
    @SuppressWarnings("rawtypes")
	public static String chuyenThanhXau(ArrayList coll) {
        StringBuffer returnBuffer = new StringBuffer("");
        Iterator iter = coll.iterator();
        while (iter.hasNext()) {
            returnBuffer.append((String)iter.next() + ",");
        }
        // Chuyen sang xau
        String returnString = returnBuffer.toString();
        // Bo dau "," o cuoi
        if (returnString.length() > 0) {
            returnString =
                    returnString.substring(0, returnString.length() - 1);
        }
        return (returnString.toString());
    }


    public static String ConvertSize(String strLength) {
        float intLength = StrToInt(strLength);
        String strSize = "";
        if (intLength <= 500F) {
            strSize = intLength + "&nbsp;Kb";
        } else {
            strSize = "&nbsp;" + intLength / 1024F;
            if (intLength % 1024F == 0.0F)
                strSize =
                        strSize.substring(0, strSize.indexOf(".")) + "&nbsp;Mb";
            else
                strSize =
                        strSize.substring(0, strSize.indexOf(".") + 2) + "&nbsp;Mb";
        }
        return strSize;
    }

    /**
     * Chuyen so double bieu dien so tien sang dang xau
     *
     * @author    Phung Duy Han
     * @param     dSo_tien - So tien dang so
     * @return        String - So tien dang xau
     * @since     04/01/2003
     */
    @SuppressWarnings("unused")
	public static String doubleToString(double dSo_tien) {
        String sSo_tien = String.valueOf(dSo_tien);
        String sGia_tri_tra_ve = "";
        int iEPos = sSo_tien.indexOf("E");
        int iCommaPos = sSo_tien.indexOf(".");
        int iSo_mu = 0;
        if (dSo_tien == 0.0D)
            return "0";
        if (sSo_tien == null)
            return "";
        if (iEPos <= 0) {
            if (sSo_tien.indexOf(".") > 0)
                sGia_tri_tra_ve = sSo_tien.substring(0, sSo_tien.indexOf("."));
            else
                sGia_tri_tra_ve = sSo_tien;
            sGia_tri_tra_ve = formatCurrency(sGia_tri_tra_ve);
            return sGia_tri_tra_ve;
        }
        iSo_mu =
                Integer.parseInt(sSo_tien.substring(iEPos + 1, sSo_tien.length()));
        if (iCommaPos <= 0) {
            sGia_tri_tra_ve = sSo_tien.substring(0, iEPos);
            for (int i = 0; i < iSo_mu; i++)
                sGia_tri_tra_ve = sGia_tri_tra_ve + "0";

        } else {
            int iSo_sau_cham = iEPos - (iCommaPos + 1);
            int iHieu_so = iSo_mu - iSo_sau_cham;
            if (iHieu_so == 0)
                sGia_tri_tra_ve =
                        sSo_tien.substring(0, iCommaPos) + sSo_tien.substring(iCommaPos +
                                                                              1,
                                                                              iEPos);
            else if (iHieu_so > 0) {
                sGia_tri_tra_ve =
                        sSo_tien.substring(0, iCommaPos) + sSo_tien.substring(iCommaPos +
                                                                              1,
                                                                              iEPos);
                for (int i = 0; i < iHieu_so; i++)
                    sGia_tri_tra_ve = sGia_tri_tra_ve + "0";

            } else {
                sGia_tri_tra_ve =
                        sSo_tien.substring(0, iCommaPos) + sSo_tien.substring(iCommaPos +
                                                                              1,
                                                                              iCommaPos +
                                                                              1 +
                                                                              iSo_mu) +
                        "." +
                        sSo_tien.substring(iCommaPos + iSo_mu + 1, iEPos);
            }
        }
        sGia_tri_tra_ve = formatCurrency(sGia_tri_tra_ve);
        return sGia_tri_tra_ve;
    }

    /**
     * Chuyen mot arraylist thanh xau chua cac
     * thanh phan duoc phan cach boi dau "'"
     *
     * @author    ManhNV
     * @param  coll (Danh sach cac id cua cac header duoc chon)
     * @return        Collection - Danh sach cac xau thanh phan     
     * @since     07/01/2004
     */    
    @SuppressWarnings("rawtypes")
	public static String chuyenThanhChuoiXau(ArrayList coll) {
        StringBuffer returnBuffer = new StringBuffer();
        Iterator iter = coll.iterator();
        while (iter.hasNext()) {
            returnBuffer.append("'" + (String)iter.next() + "'" + ",");
        }
        // Chuyen sang xau
        String returnString = returnBuffer.toString();
        if (returnString.length() > 0)
            returnString =
                    returnString.substring(0, returnString.length() - 1);
        // Bo dau "," o cuoi
        return (returnString);
    }


}
